<?php
/**
 *
 *
 * @date    2019-03-06 13:53:21
 * @version $Id$
 */

namespace App\Commons\Domain\Core;

abstract class DomainEntity
{
    public static $instance;
    protected static $entityInstance = [];
    public function __call($method, $paramters)
    {
        if (method_exists($this, $method)) {
            return $this->$method(...$paramters);
        } else {
            if (!isset(static::$entityInstance[static::class]) && defined('static::ENTITY')) {
                static::$entityInstance[static::class] = app(static::ENTITY);
            }
            $instance = static::$entityInstance[static::class] ?? null;
            if ($instance) {
                return $instance->$method(...$paramters);
            }
        }
        return null;
    }

    public static function __callStatic(string $method, array $paramters)
    {
        if (!static::$instance) {
            static::$instance = new static;
        }
        return static::$instance->{$method}(...$paramters);
    }
}
